package cn.gailvlun.gll.presentation.apartment.room;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.bean.PlaceHolder;
import cn.gailvlun.gll.chatlib.event.MatchSuccessEvent;
import cn.gailvlun.gll.databinding.ActivityMatchBinding;
import cn.gailvlun.gll.net.HttpMethods;
import cn.gailvlun.gll.net.HttpSubscriber;
import cn.gailvlun.gll.presentation.apartment.ApartmentDetailActivity;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.util.DialogUtil;
import cn.gailvlun.gll.util.RxUtil;
import cn.gailvlun.gll.widget.NavigationBarView;



public class MatchActivity extends BaseActivity {

    public static void openActivity(Context context, int memberId, int roomId) {
        Intent intent = new Intent(context, MatchActivity.class);
        intent.putExtra("memberId", memberId);
        intent.putExtra("roomId", roomId);
        context.startActivity(intent);
    }

    private ActivityMatchBinding mBinding;
    
    private int memberId;
    
    private int roomId;

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_match);
        mBinding.setPresenter(new Presenter());

        Glide.with(mContext)
                .load(R.drawable.img_match_loading)
                .apply(RequestOptions.centerCropTransform())
                .into(mBinding.ivBg);
    }

    @Override
    protected void createHandler() {
        mBinding.nbv.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {
        memberId = getIntent().getIntExtra("memberId", -1);
        roomId = getIntent().getIntExtra("roomId", -1);
        if (memberId == -1 || roomId == -1) {
            finish();
        }
    }

    @Override
    public void onBackPressed() {
        ApartmentDetailActivity.openActivity(mContext);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMatchSuccessEvent(MatchSuccessEvent event) {
        if (event.roomId == this.roomId) {
            MatchSuccessActivity.openActivity(mContext, event.cpMemberId);
            finish();
        }
    }

    public class Presenter {
        public void onCancel() {
            DialogUtil.showHint(mContext, "取消后将自动取消报名，确定取消匹配吗？", "取消匹配", "再想想", new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    if (which == DialogAction.POSITIVE) {
                        HttpMethods.getMemberService()
                                .deleteMember(memberId)
                                .compose(RxUtil.<PlaceHolder>applyScheduler())
                                .subscribe(new HttpSubscriber<PlaceHolder>() {
                                    @Override
                                    protected void onSuccess(PlaceHolder response) {
                                        ApartmentDetailActivity.openActivity(mContext);
                                    }

                                    @Override
                                    protected void onFailure(String errMsg, @Nullable PlaceHolder response, int code) {
                                        DialogUtil.showError(mContext, errMsg);
                                    }
                                });
                    }
                }
            });
        }
    }
}
